import unittest

from pyduct.handlers import *
from pytest import ProjectGatewayAFactory
from pyduct.metadata import ProjectEntryMetaDataBuilder
from pyduct.components import ProjectEntryBuilder
from pyson.gen import compactFormat

class TestProjectEntries(unittest.TestCase):

    def testProjectEntriesMessage(self):
        projectGateway = ProjectGatewayAFactory().newGateway()
        handler = JsonHandler()
        handler.projectEntryFragmentBuilders += (ProjectEntryBuilder,)
        handler.projectEntryExtensionFragmentBuilders += (ProjectEntryMetaDataBuilder,)

        entriesMsgBuilder = handler.getProjectEntriesBuilder(projectGateway)
        msgRoot = entriesMsgBuilder.build()

        output = compactFormat(entriesMsgBuilder)
        print output

        msgType = msgRoot.getStringValue("msgType")
        self.assertEquals("ProjectEntries", msgType)

        id = msgRoot.getStringValue("entries", 0, "id")
        self.assertEquals("test1", id)
        title = msgRoot.getStringValue("entries", 0, "CommonMetaData", "title")
        self.assertEquals("test1", title)
        desc = msgRoot.getStringValue("entries", 0, "CommonMetaData", "description")
        self.assertEquals("Test1 description", desc)
        extId = msgRoot.getStringValue("entries", 0, "extensions", 0, "entryType")
        self.assertEquals("java.lang.String", extId)
        extTitle = msgRoot.getStringValue("entries", 0, "extensions", 0, "CommonMetaData", "title")
        self.assertEquals("ext1", extTitle)

        id = msgRoot.getStringValue("entries", 1, "id")
        self.assertEquals("test2", id)
        title = msgRoot.getStringValue("entries", 1, "CommonMetaData", "title")
        self.assertEquals("test2", title)
        desc = msgRoot.getStringValue("entries", 1, "CommonMetaData", "description")
        self.assertEquals("Test2 description", desc)


def runTests():
    suite = unittest.TestLoader().loadTestsFromTestCase(TestProjectEntries)
    unittest.TextTestRunner(verbosity=2).run(suite)

